/*
 *  Copyright (c) 2022 ZhuHai Jieli Technology Co.,Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

.section .stack, "a"
.space 0x2000
.global _stack
_stack:


.section .init, "ax"
    .align 2
    .global _start
    .type _start,@function
    .org 0
    .extern app_load_common_code

    _start:
cpu0_start:
    call boot_info_init

    sp = _stack  ;// for idle
    ssp = _stack   ;// for interrupt
    [--sp] = {r2-r0}
//========================//
//     clear bss 		  //
//========================//
    r3 = bss_begin
    r1 = 0
    r2 = bss_size
    r2 >>= 2;
1:
    rep r2{
        [r3++] = r1
    }
    if(r2 != 0) goto 1b


//========================//
//     clear bss1 		  //
//========================//
    r3 = bss1_begin
    r1 = 0
    r2 = bss1_size
    r2 >>= 2;
1:
    rep r2{
        [r3++] = r1
    }
    if(r2 != 0) goto 1b



//========================//
//     copy data 		  //
//========================//
    r4 = data_addr
    r1 = data_begin
    r2 = data_size
    r2 >>= 2;
1:
    rep r2{
        r3 = [r1++]
        [r4++] = r3
    }
    if(r2 != 0)goto 1b


#if 0
    /* Call global constructors */
    ; r0 = __libc_fini_array
    ; call atexit
#endif

    /* Call C/C++ constructor start up code */
    // call __libc_init_array

    /* do pre-init steps before main */
//========================//
//     goto main 		  //
//========================//


    /*
     * Call vendor defined SystemInit to
     * initialize the micro-controller system
     */
    call _premain_init



    ; .extern update_result_get
        //call update_result_get

;         .extern maskrom_init
            ; call maskrom_init




    r0 = 0
    r1 = 0
    call main

    /* do post-main steps after main */

1:
    goto 1b
